const app = getApp();
var api = require('../../../../api.js');

Page({
  data: {
    id: 0,
    user_id: 0,
  },
  onLoad: function (options) {
    app.bind();
    console.log(options);
    var that = this;
    that.setData({
      id: options.id,
      user_id: options.user_id
    })
  },
  onReady: function () {
  
  },
  onShow: function () {
    this.getReceive();
  },
  onHide: function () {
  
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {
  
  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
    wx.hideShareMenu();
  },
  // 确认领取
  receive: function (e) {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url:api.card.receiveSend,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: that.data.id
      },
      success: function (e) {
        // console.log(e);
        var msg;
        if (e.data.code == 0) {
          msg = "领取成功！"
        } else if (e.data.code == 1) {
          msg = "已被领取！"
        }
        wx.showToast({
          title: msg,
          icon: 'none',
        });
        wx.switchTab({
          url: '/pages/index/index'
        })
      }
    });
  },
  // 获取基本信息
  getReceive: function () {
    console.log("——————————————————————————————");
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url:api.card.getReceive,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: that.data.id,
        user_id: that.data.user_id
      },
      success: function (e) {
        // console.log(e);
        var list = e.data.data;
        // console.log(list);
        if (e.data.code == 0) {
          that.setData({
            list: list
          })
        } else if (e.data.code == 1) {
          wx.navigateTo({
            url: '/pages/login/login'
          })
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },
})